package com.ly.controller;

import com.ly.pojo.Result;
import com.ly.utils.AliOssUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;


@RestController
public class FileUploadNetController {
    @PostMapping("/uploadNet")
    public ResponseEntity<String> upload(@RequestParam("file") MultipartFile file) {
        try {
            String originalFilename = file.getOriginalFilename();
            if (originalFilename == null) {
                throw new IllegalArgumentException("文件名为空");
            }

            int lastDotIndex = originalFilename.lastIndexOf(".");
            if (lastDotIndex == -1) {
                throw new IllegalArgumentException("文件名中没有扩展名");
            }

            String fileNameWithoutExtension = originalFilename.substring(0, lastDotIndex);

            // 其他逻辑
            String url = AliOssUtil.uploadFile1(fileNameWithoutExtension, file.getOriginalFilename());
            String s = AliOssUtil.uploadFile1(fileNameWithoutExtension, file.getInputStream().toString());
            return ResponseEntity.ok(s);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("上传失败: " + e.getMessage());
        }
    }
}

